package com.guarantee.server.common.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

/**
 * @Description List工具类
 * @Author chenli
 * @Date 2019-05-08
 * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
 */
public final class ListUtils {

    public static <T> List<T> newArrayList() {
        return newArrayList(16);
    }

    public static List<Integer> newIntArrayList(Integer... t) {
        List<Integer> ts = newArrayList(t.length);
        ts.addAll(Arrays.asList(t));
        return ts;
    }

    public static <T> List<T> newArrayList(T... t) {
        List<T> ts = newArrayList(t.length);
        ts.addAll(Arrays.asList(t));
        return ts;
    }

    public static <T> List<T> newArrayList(int size) {
        return new ArrayList<>(size);
    }

    public static <T, R> List<T> newArrayList(List<R> list, Function<R, T> consumer) {
        List<T> ts = new ArrayList<>(list.size());
        list.forEach(r -> ts.add(consumer.apply(r)));
        return ts;
    }

    /**
     * 判断list是否为空
     *
     * @param list:判断对象
     * @return 为空则为true
     */
    public static <T> boolean isEmpty(List<T> list) {
        if (null == list) {
            return true;
        }
        return list.isEmpty();
    }

    /**
     * 判断list是否为空
     *
     * @param list:判断对象
     * @return 为空则为true
     */
    public static <T> boolean isEmptyOrItemNull(List<T> list) {
        if (null == list) {
            return true;
        }
        if (list.isEmpty()) {
            return true;
        }
        if (list.get(0) == null) {
            return true;
        }
        return false;
    }

    public static <T> boolean isNotEmpty(List<T> list) {
        if (null == list) {
            return false;
        }
        return !list.isEmpty();
    }

    /**
     * 判断list是否为空
     *
     * @param list:判断对象
     * @return 为空则为true
     */
    public static <T> boolean isNotEmptyOrItemNotNull(List<T> list) {
        return !isEmptyOrItemNull(list);
    }

    /**
     * 数组转为list
     *
     * @param ts  :
     * @param <T> :
     * @return :
     */
    public static <T> List<T> arrayAsList(T[] ts) {
        return Arrays.asList(ts);
    }

}